﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wheeler.Domain
{
    /// <summary>
    /// This Call Will Be Used to Contain the Extension Methods of Entity Context Class
    /// </summary>
    public static class ContextExtensions
    {
        /// <summary>
        /// Determines whether the underlying database connection is exist and operational
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static bool IsConnected(this WheelerEntities context)
        {
            try
            {
                int count = context.Groups.Count();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return false;
        }

        #region Get Commonly Used Entities

        ///// <summary>
        ///// Gets Company object by ID with Address and Contacts object preloaded
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public static Company GetCompanyByID(this WheelerEntities context, int id)
        //{
        //    return context.Companies
        //        .Include("Address")
        //        .Include("Contacts")
        //        .FirstOrDefault(P => P.ID == id);
        //}

        ///// <summary>
        ///// Gets a JobSeeker Object with Address and Resume Preloaded
        ///// </summary>
        ///// <param name="context"></param>
        ///// <param name="jobSeekerId"></param>
        ///// <returns></returns>
        //public static JobSeeker GetJobSeekerByID(this WheelerEntities context, int jobSeekerId)
        //{
        //    return context.JobSeekers
        //        .Include("Address")
        //        .Include("Resumes")
        //        .FirstOrDefault(P => P.ID == jobSeekerId);
        //}

        ///// <summary>
        ///// Gets Job By Id with related child objects 
        ///// Like Category, City, Company
        ///// </summary>
        ///// <param name="context"></param>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public static Job GetJobByID(this WheelerEntities context, int id)
        //{
        //    return context.Jobs
        //        .Include("Category")
        //        .Include("City")
        //        .Include("Company")
        //        .FirstOrDefault(P => P.ID == id);
        //}
        #endregion

        #region Check for Duplicates
        
        /// <summary>
        /// Checks Wheather a Vehicle Registraion is Already registered in the system.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="registrationNo"></param>
        /// <returns></returns>
        public static bool IsDuplicateRegistrationNumber(this WheelerEntities context, String registrationNo, int vehicleID)
        {
            if (vehicleID == 0)
            {
                if (context.Vehicles.FirstOrDefault(P => String.Compare(P.RegistrationNo, registrationNo, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Vehicles.FirstOrDefault(P => P.ID != vehicleID && String.Compare(P.RegistrationNo, registrationNo, true) == 0) != null)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks wheather a Engine Number is already checked or not.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="engineNumber"></param>
        /// <returns></returns>
        public static bool IsDuplicateEngineNumber(this WheelerEntities context, string engineNumber, int vehicleID)
        {
            if (vehicleID == 0)
            {
                if (context.Vehicles.FirstOrDefault(P => String.Compare(P.EngineNo, engineNumber, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Vehicles.FirstOrDefault(P => P.ID != vehicleID && String.Compare(P.EngineNo, engineNumber, true) == 0) != null)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks wheather a Engine Number is already checked or not.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="engineNumber"></param>
        /// <returns></returns>
        public static bool IsDuplicateChasisNumber(this WheelerEntities context, string chasisNumber, int vehicleID)
        {
            if (vehicleID == 0)
            {
                if (context.Vehicles.FirstOrDefault(P => String.Compare(P.ChasisNo, chasisNumber, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Vehicles.FirstOrDefault(P => P.ID != vehicleID && String.Compare(P.ChasisNo, chasisNumber, true) == 0) != null)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks wheather a Engine Number is already checked or not.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="engineNumber"></param>
        /// <returns></returns>
        public static bool IsDuplicateCylinderNumber(this WheelerEntities context, string cylinderNumber, int vehicleID)
        {
            if (vehicleID == 0)
            {
                if (context.Vehicles.FirstOrDefault(P => String.Compare(P.CNGCylinderNo, cylinderNumber, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Vehicles.FirstOrDefault(P => P.ID != vehicleID && String.Compare(P.CNGCylinderNo, cylinderNumber, true) == 0) != null)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks for a duplicate BRTA number
        /// </summary>
        /// <param name="context"></param>
        /// <param name="BRTANumber"></param>
        /// <param name="vehicleID"></param>
        /// <returns></returns>
        public static bool IsDuplicateBRTANumber(this WheelerEntities context, string BRTANumber, int vehicleID)
        {
            if (vehicleID == 0)
            {
                if (context.Vehicles.FirstOrDefault(P => String.Compare(P.BRTANumber, BRTANumber, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Vehicles.FirstOrDefault(P => P.ID != vehicleID && String.Compare(P.BRTANumber, BRTANumber, true) == 0) != null)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks wheather a vehicle information exists in Fixed Vehicle table, imported from BRTA. 
        /// If exists then returns the corresponding object otherwise returns null;
        /// </summary>
        /// <param name="context"></param>
        /// <param name="registrationNo"></param>
        /// <param name="chassisNo"></param>
        /// <param name="engineNo"></param>
        /// <param name="vehicleID"></param>
        /// <returns></returns>
        public static FixedVehicle GetFixedVehicle(this WheelerEntities context, String registrationNo, String chassisNo, String engineNo)
        {
            FixedVehicle fixedVehicle = null;
            fixedVehicle = context.FixedVehicles.FirstOrDefault(P => (String.Compare(P.RegistrationNo, registrationNo, true) == 0
                && String.Compare(P.ChassisNo, chassisNo, true) == 0
                && String.Compare(P.EngineNo, engineNo, true) == 0));
            
            return fixedVehicle;
        }

        /// <summary>
        /// Checks whethear a UserName is already taken or not.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userName"></param>
        /// <param name="userID">The User ID that will be excluded for duplicate checking. Pass 0 to check in all records</param>
        /// <returns></returns>
        public static bool IsDuplicateUserName(this WheelerEntities context, string userName, int userID)
        {
            if (userID == 0)
            {
                if (context.Users.FirstOrDefault(P => String.Compare(P.UserName, userName, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Users.FirstOrDefault(P => P.ID != userID && String.Compare(P.UserName, userName, true) == 0) != null)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks wheather a serial number is already present or not
        /// </summary>
        /// <param name="context"></param>
        /// <param name="serialNumber"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsDuplicateSerialNumber(this WheelerEntities context, String serialNumber, int id)
        {
            if (id == 0)
            {
                if (context.TestResults.FirstOrDefault(P => String.Compare(P.SerialNumber, serialNumber, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.TestResults.FirstOrDefault(P => P.ID != id && String.Compare(P.SerialNumber, serialNumber, true) == 0) != null)
                    return true;
            }
            return false;
        }
        #endregion Check for Duplicates
    }
}
